Component({

    /**
     * 页面的初始数据
     */
    data: {
        biddingList:[],
        currentPage: 1,
        loadAll: false,
        loadingSuc: false, 
    },

    ready(options) {
        console.log("重新执行onload")
        this.setData({
            biddingList: [],
            currentPage:1,
            loadAll: false,
            loadingSuc: false
        })
        this.getInitData(1);
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        if(this.data.loadAll) return
        this.setData({
            currentPage: this.data.currentPage + 1
        })
        console.log("触底触底",this.data.currentPage)
        this.getInitData(this.data.currentPage)
    },

    methods: {
        /**
         * 页面上拉触底事件的处理函数
         */
        turnPage() {
            console.log(666666666)
            if(this.data.loadAll) return
            this.setData({
                currentPage: this.data.currentPage + 1
            })
            console.log("翻页bidding",this.data.currentPage)
            this.getInitData(this.data.currentPage)
        },
        /**
         * 
         * 刷新页面
         */
        reflesh() {
            console.log("刷新Bidding")
            this.setData({
                biddingList: [],
                currentPage:1,
                loadAll: false,
                loadingSuc: false
            })
            this.getInitData(1);
        },
        /**
         * 初始化数据
         */
        getInitData(page){
            getApp().network.GET({
                url:"/new/bid/list",
                params:{
                    page:page
                },
                success: async (res)=>{
                    console.log(res)
                    if(res.data.code === 0){
                        let tempList = res.data.data;
                        var nowTime = await getApp().getServeTime();
                        tempList.forEach( item=>{
                            item.total_amount = parseFloat(item.total_amount); //去除小数点后多余0
                            item.start_price = parseFloat(item.start_price); 
                            if(nowTime < item.start_time){
                                item.status = "未开始";
                                item.time_text = `开始时间：${this.formatTime(item.start_time)}`;
                            }else if(nowTime>= item.start_time && nowTime <= item.dynamic_end_time){
                                item.status = "进行中";
                                item.time_text = `结束时间：${this.formatTime(item.dynamic_end_time)}`;
                            }else{
                                item.status = "已结束";
                                item.time_text = `结束时间：${this.formatTime(item.dynamic_end_time)}`;
                            }
                        })
                        if(tempList.length <10){ //!!!单页十个，否则代表加载完了不用翻页
                            this.setData({
                                biddingList: this.data.biddingList.concat(tempList),
                                loadAll: true,
                                loadingSuc: true
                            })
                        }else{
                            this.setData({
                                biddingList: this.data.biddingList.concat(tempList),
                                loadingSuc: true
                            })
                        }
                    }
                    wx.stopPullDownRefresh()
                },
                fail: (err)=>{
                    console.log(err)
                }
            })
        },
        /**
         * 格式化时间戳为09-02 10：00：00格式
         */
        formatTime(num){
            var date = new Date((num+"000"-0) + 8*60*60*1000);
            var YY = date.getFullYear();
            var MM = (date.getMonth()+1)<10 ? "0"+(date.getMonth()+1) : date.getMonth()+1;
            var DD = date.getDate()<10 ? "0"+date.getDate() : date.getDate();
            var hh = date.getHours()<10 ? "0"+date.getHours() : date.getHours();
            var mm = date.getMinutes()<10 ? "0"+date.getMinutes() : date.getMinutes();
            var ss = date.getSeconds()<10 ? "0"+date.getSeconds() : date.getSeconds();
            return YY+"-"+MM+"-"+DD+" "+hh+":"+mm+":"+ss
        },
        /**
         * 跳转详情页
         */
        toDetail(e){
            wx.navigateTo({
                url: '/pages/bidding_detail/bidding_detail?id='+e.currentTarget.dataset.id,
            })
        }
    }
})